package cn.edu.laud.sany.generatedata;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.edu.laud.sany.utils.MyLog;

public class SanyCSVReader extends Thread
{
	protected MyLog logger;
	protected SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	protected SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
	protected static List<RowData> rowList = new ArrayList<RowData>();
	protected static int count = 0;
	
	public SanyCSVReader(int no)
	{
		logger = MyLog.getMyLog(SanyGenerateData.getLogFilePath(), true);
		logger.setThreadName("Thread " + no);
	}
	
	private String trimData(String oriData)
	{
		oriData = oriData.trim();
		oriData = oriData.substring(1, oriData.length() - 1);
		return oriData;
	}
	
	protected RowData getRowData(String string)
	{
		String data[] = null;
		String equipCode = null;
		int paraId = 0;
		String paravalue = null;
		Date localTime = null;
		try
		{
			data = string.split(",");
			if (data.length != 6)
			{
				logger.error("data format error.(org:)" + string);
				System.err.println("data format error.(org:)" + string);
				return null;
			}
			equipCode = trimData(data[1]);
			paraId = Integer.valueOf(trimData(data[2]));
			if (paraId == 356)
			{
				return null;
			}
			paravalue = trimData(data[3]);
			String dataString = trimData(data[5]);
			try
			{
				localTime = sdf.parse(dataString);
			}
			catch (ParseException e)
			{
				try
				{
					localTime = sdf2.parse(dataString);
				}
				catch (ParseException e1)
				{
					e1.printStackTrace();
					logger.error(e1.getMessage() + "(ori:)" + string);
					return null;
				}
			}
		}
		catch (NumberFormatException e1)
		{
			e1.printStackTrace();
			logger.error(e1.getMessage() + "(ori:)" + string);
			return null;
		}
		RowData rowData = new RowData();
		rowData.paraId = paraId;
		rowData.paravalue = paravalue;
		rowData.equipCode = equipCode;
		rowData.localTime = localTime;
		return rowData;
	}
	
	protected long readData(File file)
	{
		BufferedReader bReader = null;
		try
		{
			try
			{
				bReader = new BufferedReader(new FileReader(file));
			}
			catch (FileNotFoundException e3)
			{
				e3.printStackTrace();
				logger.error(e3.getMessage());
				return 0;
			}
			
			try
			{
				String lineData = bReader.readLine();
				while ((lineData = bReader.readLine()) != null)
				{
					RowData rowData = getRowData(lineData);
					if (rowData == null)
					{
						continue;
					}
					count++;
					rowList.add(rowData);
					if (count % 100000 == 0)
					{
						logger.info("read count = " + count);
						SanyGenerateData.dealwithRowData(rowList);
					}
					if (count > SanyGenerateData.readDataLimit)
					{
						break;
					}
				}
			}
			catch (IOException e1)
			{
				e1.printStackTrace();
				logger.error(e1.getMessage());
			}
			finally
			{
				try
				{
					SanyGenerateData.dealwithRowData(rowList);
					bReader.close();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				catch (NullPointerException e1)
				{
					e1.printStackTrace();
					logger.error(e1.getMessage());
				}
			}
		}
		catch (Throwable t)
		{
			t.printStackTrace();
			logger.error(t.getMessage());
		}
		
		return count;
	}
	
	@Override
	public void run()
	{
		logger.info("thread start");
		while (true)
		{
			File file = SanyGenerateData.getAvailableSource();
			if (file == null)
			{
				break;
			}
			String fileName = file.getAbsolutePath();
			logger.info("find a new input file: " + fileName);
			// import
			long total = readData(file);
			logger.info("read finished! count = " + total);
		}
		logger.info("thread end");
	}
	
}
